@isTest
private class ContractProductAdjustControllerTest {
    static testMethod void testMethod1() {
        Account acc = TestUtils.createAccount();
        insert acc;
        
        Contract__c con = new Contract__c(
           Corpcode__c = 'MKSY',
            Account__c = acc.Id,
            TerminalCustomer__c = acc.Id,
            Status__c = '已生效'
        );
        insert con;
        ProductStructure__c ps = new ProductStructure__c();
        ps.Classify3__c = '试剂';
        insert ps;
        Product__c p1 = TestUtils.createProduct();
        p1.Category0__c = '试剂';
        p1.ProductStructure__c = ps.Id;
        
        Product__c p2 = TestUtils.createProduct();
        p2.Category0__c = '试剂';
        p2.ProductStructure__c = ps.Id;
        insert new Product__c[] {p1, p2};
  
        Price__c pr1 = new Price__c(
            Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 1.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 1.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 1.0
            
        );
        Price__c pr2 = new Price__c(
            Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 2.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 2.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 1.0
            
        );
        insert new Price__c[] {pr1,pr2};
        
           /// 日期处理
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        AveragePrice__c av1 = new AveragePrice__c(
        Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
          AveragePrice__c av2 = new AveragePrice__c(
        Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
        insert new AveragePrice__c[] {av2,av1}; 

        ContractProduct__c cp1 = new ContractProduct__c(
            Product__c = p1.Id,
            Contract__c = con.Id,
            Only__c = 'cp1'
        );
        ContractProduct__c cp2 = new ContractProduct__c(
            Product__c = p2.Id,
            Contract__c = con.Id,
            Only__c = 'cp2'
        );
        insert new ContractProduct__c[] {cp1, cp2};

        // 画面测试
        PageReference page = new PageReference('/apex/ContractProductAdjust?accid=' + acc.Id);
        System.Test.setCurrentPage(page);
        ContractProductAdjustController co = new ContractProductAdjustController(new ApexPages.StandardController(new AdjustPrice__c()));

        co.init();
       ///System.debug('测试123'+co.cpList.size());
        co.accId = acc.Id;
        co.category = '临检';
        co.product = '尿有形成份';
        co.brand = '希森美康';
        co.serialNo='ZG801009';
        co.search();
        
        co.cpList[0].chk = true;
        co.cpList[1].chk = true;
        System.debug('+++++保存');
        co.exchange();
        co.selectCPList[0].adjustSalesprice = 999;
        co.selectCPList[1].adjustSalesprice = 888;

        co.save();
        List<AdjustPrice__c> ap = [select Id from AdjustPrice__c];
        System.assertEquals(1, ap.size());

        // Trigger测试
        ap[0].Status__c = '已批准';
        update ap[0];
////回写到合同产品
        AdjustPriceHandler.adjustPrice(ap[0].Id);


        List<ContractProduct__c> cpList2 = [select SalesPrice__c from ContractProduct__c order by Id];
       // System.debug(cpList2[0].SalesPrice__c);
       // System.debug(cpList2[1].SalesPrice__c);
        
        System.assertEquals(999, cpList2[0].SalesPrice__c);
        System.assertEquals(888, cpList2[1].SalesPrice__c);

        co.adjustId = ap[0].Id;
        co.accId = null;
        co.init();
    }
}